package com.hkts.naturalgas.organizational.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.feign.FeignInterface;
import com.hkts.naturalgas.organizational.entity.HontDepartment;
import com.hkts.naturalgas.organizational.entity.HontUser;
import com.hkts.naturalgas.organizational.mapper.HontDepartmentMapper;
import com.hkts.naturalgas.organizational.mapper.HontUserMapper;
import com.hkts.naturalgas.organizational.orgVo.DepartmentAndUserVo;
import com.hkts.naturalgas.organizational.orgVo.DepartmentsAndIdentityVo;
import com.hkts.naturalgas.organizational.orgVo.HontDepartmentAndUserVo;
import com.hkts.naturalgas.organizational.orgVo.PossessDepartVo;
import com.hkts.naturalgas.organizational.service.IHontDepartmentService;
import com.hkts.naturalgas.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2020-12-23
 */
@Service
@Transactional
public class HontDepartmentServiceImpl extends ServiceImpl<HontDepartmentMapper, HontDepartment> implements IHontDepartmentService {

    @Autowired
    private HontUserMapper hontUserMapper;
    @Autowired
    private HontDepartmentMapper hontDepartmentMapper;
    @Autowired
    private    FeignInterface feignInterface;
    @Override
    public IPage<HontDepartment> selectPage(Page<HontDepartment> page, HontDepartment hontDepartment){
        QueryWrapper<HontDepartment> query = Wrappers.query();
        QueryWrapper<HontDepartment> entityWrapper = getEntityWrapper(query, hontDepartment);
        return hontDepartmentMapper.selectPage(page, entityWrapper);
        }




    @Override
    public List<HontDepartment> selectList(HontDepartment hontDepartment){
        QueryWrapper<HontDepartment> query = Wrappers.query();
        QueryWrapper<HontDepartment> entityWrapper = getEntityWrapper(query, hontDepartment);
        return hontDepartmentMapper.selectList(entityWrapper);
        }
    /*
    * 根据当前权限查找所有人和部门
    * */

    @Override
    public  List<HontDepartmentAndUserVo> getHontUserAndTeam() {
        List<String> departmentIds = feignInterface.getDepartmentIds();
        List<HontDepartmentAndUserVo> lietVo=new ArrayList<>();
        Result<Map<String, Object>> result = feignInterface.performanceUserInfo();
        Map<String, Object> result1 = result.getResult();
        String identity = result1.get("identity").toString();
        if(departmentIds.size()==0){
            return lietVo;
        }else{
            DepartmentsAndIdentityVo vo=new DepartmentsAndIdentityVo();
            vo.setDepartments(departmentIds);
            vo.setIdentity(identity);
           lietVo = hontDepartmentMapper.getDepartmentsAndUsersByLoginUser(vo);
            lietVo.stream().forEach(hontDepartmentAndUserVo -> {
                hontDepartmentAndUserVo.setUserNum(hontDepartmentAndUserVo.getHontUsers().size());
            });
            /*List<HontDepartment> departmentsByLoginUser = hontDepartmentMapper.getDepartmentsByLoginUser(departmentIds);
            List<HontUser> hontUserByLoginUser = hontUserMapper.getHontUserByLoginUser(departmentIds);
            departmentsByLoginUser.stream().forEach(de->{
                DepartmentAndUserVo vo=new DepartmentAndUserVo();
                vo.setDepartmentId(de.getId());
                vo.setDepartmentName(de.getTitle());
                for(int i=0;i<hontUserByLoginUser.size();i++){
                    if(hontUserByLoginUser.get(i).getDepartmentCopyId().equals(de.getId())){
                        vo.getHontUser().add(hontUserByLoginUser.get(i)) ;
                    }
                }
                vo.setUserNum(vo.getHontUser().size());
                lietVo.add(vo);
            });*/


        }


        return lietVo;
    }

    @Override
    public  List<PossessDepartVo> getHontUserPossess(){
        List<String> departmentIds = feignInterface.getDepartmentIds();
        Result<Map<String, Object>> result = feignInterface.performanceUserInfo();
        Map<String, Object> result1 = result.getResult();
        String identity = result1.get("identity").toString();
        int level = Integer.parseInt(identity);
        List<PossessDepartVo> possessDepartVos = new ArrayList<>();
        if (level == 2){
            if (departmentIds.size() > 0){
                List<PossessDepartVo> departmentsByLoginUser = hontDepartmentMapper.getDepartmentsByLoginUser(departmentIds);
                PossessDepartVo departVo = new PossessDepartVo();
                departVo.setDepartType(0);
                departVo.setDepartName("个人");
                departVo.setDepartId("1");
                possessDepartVos.add(departVo);
                departmentsByLoginUser.forEach(possessDepartVo->{
                    if (possessDepartVo.getDepartId() != null){
                        possessDepartVo.setDepartType(1);
                    }
                });
                possessDepartVos.addAll(departmentsByLoginUser);
            }else {
                PossessDepartVo departVo = new PossessDepartVo();
                departVo.setDepartType(0);
                departVo.setDepartName("个人");
                departVo.setDepartId("1");
                possessDepartVos.add(departVo);
            }
        }else if (level > 2 && level < 6){
            if (departmentIds.size() > 0){
                List<PossessDepartVo> departmentsByLoginUser = hontDepartmentMapper.getDepartmentsByLoginUser(departmentIds);
                PossessDepartVo departVo = new PossessDepartVo();
                departVo.setDepartType(2);
                departVo.setDepartName("全部");
                departVo.setDepartId("2");
                possessDepartVos.add(departVo);
                departmentsByLoginUser.forEach(possessDepartVo->{
                    if (possessDepartVo.getDepartId() != null){
                        possessDepartVo.setDepartType(1);
                    }
                });
                possessDepartVos.addAll(departmentsByLoginUser);
            }else {
                PossessDepartVo departVo = new PossessDepartVo();
                departVo.setDepartType(2);
                departVo.setDepartName("全部");
                departVo.setDepartId("2");
                possessDepartVos.add(departVo);
            }
        }
        return possessDepartVos;
    }

    /**
    *  公共查询条件
    * @param entityWrapper
    * @return
    */
    public QueryWrapper<HontDepartment> getEntityWrapper(QueryWrapper<HontDepartment> entityWrapper,HontDepartment hontDepartment){
        //条件拼接
                if(StringUtils.isNotBlank(hontDepartment.getCreateBy())){
                entityWrapper.like(HontDepartment.CREATE_BY,hontDepartment.getCreateBy());
                }
               /* if(StringUtils.isNotBlank(hontDepartment.getCreateTime())){
                entityWrapper.like(HontDepartment.CREATE_TIME,hontDepartment.getCreateTime());
                }
                if(StringUtils.isNotBlank(hontDepartment.getDelFlag())){
                entityWrapper.like(HontDepartment.DEL_FLAG,hontDepartment.getDelFlag());
                }*/
                if(StringUtils.isNotBlank(hontDepartment.getUpdateBy())){
                entityWrapper.like(HontDepartment.UPDATE_BY,hontDepartment.getUpdateBy());
                }
              /*  if(StringUtils.isNotBlank(hontDepartment.getUpdateTime())){
                entityWrapper.like(HontDepartment.UPDATE_TIME,hontDepartment.getUpdateTime());
                }*/
                if(StringUtils.isNotBlank(hontDepartment.getParentId())){
                entityWrapper.like(HontDepartment.PARENT_ID,hontDepartment.getParentId());
                }
               /* if(StringUtils.isNotBlank(hontDepartment.getSortOrder())){
                entityWrapper.like(HontDepartment.SORT_ORDER,hontDepartment.getSortOrder());
                }
                if(StringUtils.isNotBlank(hontDepartment.getStatus())){
                entityWrapper.like(HontDepartment.STATUS,hontDepartment.getStatus());
                }
                if(StringUtils.isNotBlank(hontDepartment.getTitle())){
                entityWrapper.like(HontDepartment.TITLE,hontDepartment.getTitle());
                }
                if(StringUtils.isNotBlank(hontDepartment.getParent())){
                entityWrapper.like(HontDepartment.IS_PARENT,hontDepartment.getParent());
                }*/
        return entityWrapper;
    }
    @Override
    public Boolean isParent(String departmentId){
        return hontDepartmentMapper.getIsParent(departmentId);
    }

    @Override
    public List<HontDepartment> getAllParentDepart(){
        QueryWrapper<HontDepartment> query = Wrappers.query();
        query.lambda().eq(HontDepartment::getParentId,0);
        List<HontDepartment> hontDepartments = hontDepartmentMapper.selectList(query);
        return hontDepartments;
    }
}
